//
//  ChartView.swift
//  KoiAccounting
//
//  Created by 纪洪文 on 23.03.25.
//

import SwiftUI

struct ChartView: View {
    
    @StateObject var viewModel = ChartViewModel()
    
    var body: some View {
        VStack {
            ZStack {
                KoiTabBar<DateSelectType>(selection: $viewModel.dateSelectType, items: [
                    TabBarItem(tab: DateSelectType.week, name: DateSelectType.week.typeName()),
                    TabBarItem(tab: DateSelectType.month, name: DateSelectType.month.typeName()),
                    TabBarItem(tab: DateSelectType.year, name: DateSelectType.year.typeName()),
                ]).padding(.horizontal, 20)
                
                HStack {
                    Menu {
                        Button("支出", action: {
                            viewModel.categoryType = .expense
                        })
                        Button("收入", action: {
                            viewModel.categoryType = .income
                        })
                    } label: {
                        if (viewModel.categoryType == .income) {
                            HStack {
                                Text("收入")
                                    .font(.system(size: 14))
                                    .bold()
                                    .foregroundColor(Color.koiColorTitle)
                                Image("koi_ic_arrow_down")
                            }.padding(.horizontal, 20)
                        } else {
                            HStack {
                                Text("支出")
                                    .font(.system(size: 14))
                                    .bold()
                                    .foregroundColor(Color.koiColorTitle)
                                Image("koi_ic_arrow_down")
                            }.padding(.horizontal, 20)
                        }
                    }
                    Spacer()
                }
            }
            TabView(selection: $viewModel.dateSelectType) {
                ChartDateWeekView(date: $viewModel.weekDate, categoryType: $viewModel.categoryType).tag(DateSelectType.week)
                ChartDateMonthView(date: $viewModel.monthDate, categoryType: $viewModel.categoryType).tag(DateSelectType.month)
                ChartDateYearView(date: $viewModel.yearDate, categoryType: $viewModel.categoryType).tag(DateSelectType.year)
            }.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
        }
    }
}

#Preview {
    ChartView()
}
